<template>
  <div>
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form-model
        ref="formModel"
        :model="form"
        layout="inline"
        :rules="{
          name: [
            {
              validator: validators.noWhitespace(),
              trigger: 'change',
            },
            {
              validator: validators.noIllegalString(),
              trigger: 'change',
            },
            {
              max: 10,
              message: '长度在 1 到 10 个字符',
              trigger: 'change',
            },
          ],
        }"
      >
        <a-row :gutter="[16, 16]" type="flex" justify="end">
          <a-col :md="6" :sm="8">
            <a-form-model-item prop="name" label="应用名称">
              <a-input autocomplete="off" v-model="form.name" />
            </a-form-model-item>
          </a-col>
          <span
            style="float: left; overflow: hidden"
            class="table-page-search-submitButtons"
          >
            <a-col :md="12" :sm="24">
              <a-button
                type="primary"
                @click="loadData(true)"
                icon="search"
                style="margin-left: 21px"
                >查询</a-button
              >
              <a-button
                @click="resetSearchForm"
                icon="reload"
                style="margin-left: 8px"
                >重置</a-button
              >
            </a-col>
          </span>
        </a-row>
      </a-form-model>
    </div>

    <a-table
      ref="table"
      rowKey="id"
      size="middle"
      :columns="columns"
      :dataSource="gDataSource"
      :pagination="gPagination"
      @change="gHandleTableChange"
    >
      <span slot="sortCol" slot-scope="text, record, index">
        {{
          (gPagination.current - 1) * gPagination.pageSize + parseInt(index) + 1
        }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a-popconfirm title="确定执行吗?" @confirm="() => handleAction(record)">
          <a-button type="primary" size="small" icon="caret-right"
            >执行</a-button
          >
        </a-popconfirm>
        <a-divider type="vertical" />
        <a-button
          type="primary"
          size="small"
          icon="notification"
          @click="handleForecast(record)"
          >效果预测</a-button
        >
        <!-- <a @click="handleAction(record)">执行</a> -->
      </span>
      <span
        slot="cloudType"
        slot-scope="text, record"
        style="vertical-align: middle"
      >
        <template v-if="record.schedulerModelTool === 0">
          <img src="~@/assets/k8s_logo.svg" alt="logo" style="width: 18px" />
        </template>
        <template v-else-if="record.schedulerModelTool === 2">
          <img src="~@/assets/huawei_logo.png" alt="logo" style="width: 18px" />
        </template>
        <template v-else>
          <img src="~@/assets/aliyun_logo.png" alt="logo" style="width: 18px" />
        </template>
        {{ text }}
      </span>
    </a-table>
    <forecast-modal ref="forecastModal"></forecast-modal>
  </div>
</template>

<script>
import ForecastModal from "./ForecastModal";
import { GwListMixin } from "@/mixins/GwListMixin";
import { gPostAction } from "@/api/manage";

export default {
  name: "Page",
  mixins: [GwListMixin],
  components: {
    ForecastModal,
  },
  props: {
    model: {
      type: String,
      default: undefined,
    },
  },
  data() {
    return {
      // 表头
      columns: [
        {
          title: "序号",
          dataIndex: "",
          key: "rowIndex",
          width: 50,
          scopedSlots: { customRender: "sortCol" },
        },
        {
          title: "应用名称",
          dataIndex: "appName",
          // width: "80px",
        },
        {
          title: "调度实例",
          dataIndex: "instanceName",
          // width: "50px",
          scopedSlots: { customRender: "cloudType" },
        },
        {
          title: "原数据中心",
          dataIndex: "srcDatacenterName",
          // width: "90px",
        },
        {
          title: "目标数据中心",
          dataIndex: "dstDatacenterName",
          // width: "110px",
        },
        // {
        //   title: "调度模型",
        //   dataIndex: "schedulerModel",
        //   // width: "80px",
        //   customRender: (text) => this.schedulerModelTypeMap[text],
        // },
        {
          title: "迁移工具",
          dataIndex: "schedulerModelTool",
          // width: "80px",
          customRender: (text) => this.schedulerModelToolTypeMap[text],
        },
        {
          title: "迁移策略",
          // width: "80px",
          dataIndex: "schedulerStrategyConfig",
          customRender: (text) => this.schedulerStrategyConfigTypeMap[text],
        },
        {
          title: "操作",
          width: "200px",
          dataIndex: "action",
          scopedSlots: { customRender: "action" },
        },
      ],
      schedulerModelTypeMap: ["最优成本代价模型", "最优设备代价模型"],
      schedulerModelToolTypeMap: [
        "Kubernetes迁移工具",
        "阿里云迁移工具",
        "华为迁移工具",
      ],
      schedulerStrategyConfigTypeMap: ["全量迁移", "模版迁移"],
      url: {
        list: "/scheduler/job/advicePage",
      },
      form: {
        name: "",
      },
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    handleForecast(data) {
      this.$refs.forecastModal.showModal(data);
    },
    loadData(isDoSearch) {
      this.$refs.formModel.validate((error) => {
        if (!error) {
          return;
        }

        this.gQueryParam.modelType = this.model;

        this.gQueryParam.name = this.form.name;
        this.gQueryParam.title = "多数据中心调度-动态调度机制-调度建议";
        this.gQueryParam.content = "查询调度建议列表";
        this.gQueryParam.status = this.form.status;
        if (isDoSearch) {
          this.gQueryParam.pageNo = 1;
          this.gPagination.current = 1;
        }
        gPostAction(this.url.list, this.gQueryParam).then((res) => {
          if (res.success) {
            this.gDataSource = res.data.list;
            this.gPagination.total = res.data.totalCount;
          }
        });
      });
    },
    handleAction(record) {
      const postData = {
        adviceId: record.id,
      };

      gPostAction("/scheduler/job/startAdvice", postData).then((res) => {
        if (res.success) {
          this.$message.success("操作成功");
          this.$emit("ok");
          this.loadData();
        }
      });
    },
    resetSearchForm() {
      this.form.name = "";
      this.$refs.formModel.resetFields();
    },
  },
};
</script>